package com.yyge;

import com.google.common.collect.Sets;
import com.yyge.entity.User;

import java.util.Set;

/**
 * 简要：可以求交集/并集/补集/差集的工具
 * 实现集合的并集/交集/补集/差集 的工具包
 */
public class SetsTest {
    public static void main(String[] args) {


        //普通数字 求并集/交集/补集/差集
        // num();

        //对象 求并集/交集/补集/差集
        obj();
    }

    /**
     * 普通数字 求并集/交集/补集/差集
     */
    public static void num(){
        Set<Integer> set1 = Sets.newHashSet(1, 2, 4, 5, 6, 8);
        Set<Integer> set2 = Sets.newHashSet(2, 3, 4, 5, 6, 7, 9);
        System.out.println("集合1:"+set1);
        System.out.println("集合2:"+set2);

        //合集，并集   [1, 2, 4, 5, 6, 8, 3, 7, 9]
        Set<Integer> result1 = Sets.union(set1, set2);
        //交集          [2, 4, 5, 6]
        Set<Integer> result2 = Sets.intersection(set1, set2);
        //差集 1中有而2中没有的  [1, 8]
        Set<Integer> result3 = Sets.difference(set1, set2);
        //相对差集 1中有2中没有  2中有1中没有的 取出来做结果 [1, 8, 3, 7, 9]
        Set<Integer> result4 = Sets.symmetricDifference(set1, set2);

        System.out.println("并集:"+result1);
        System.out.println("交集:"+result2);
        System.out.println("差集:"+result3);
        System.out.println("相对差集:"+result4);
    }

    /**
     * 对象 求并集/交集/补集/差集
     */
    public static void obj(){
        User user1 = new User("王一",18,"11");
        User user2 = new User("赵二",21,"22");
        User user3 = new User("张三",31,"33");
        User user4 = new User("李四",18,"44");
        User user5 = new User("王五",22,"55");
        User user6 = new User("赵六",13,"66");
        User user7 = new User("王一",18,"77");
        User user8 = new User("赵铁柱",8,"88");
        User user9 = new User("李大纲",25,"99");
        Set<User> set1 = Sets.newHashSet(user1, user3, user5, user7, user2, user6);
        Set<User> set2 = Sets.newHashSet(user1, user2, user3, user4, user8, user9, user6);

        //合集，并集   [1, 2, 4, 5, 6, 8, 3, 7, 9]
        Set<User> result1 = Sets.union(set1, set2);
        //交集          [2, 4, 5, 6]
        Set<User> result2 = Sets.intersection(set1, set2);
        //差集 1中有而2中没有的  [1, 8]
        Set<User> result3 = Sets.difference(set1, set2);
        //相对差集 1中有2中没有  2中有1中没有的 取出来做结果 [1, 8, 3, 7, 9]
        Set<User> result4 = Sets.symmetricDifference(set1, set2);

        System.out.println("并集:"+result1);
        System.out.println("交集:"+result2);
        System.out.println("差集:"+result3);
        System.out.println("相对差集:"+result4);
    }
}
